const router = require("koa-router")();
const OpenAI = require("openai");

router.get("/doubao", async (ctx, next) => {
  const { content } = ctx.request.query
  const openai = new OpenAI({
    apiKey: "49a54fd7-e579-4df6-992b-a9496afa3f00",
    baseURL: "https://ark.cn-beijing.volces.com/api/v3",
  });

  async function main() {
    // Non-streaming:
    console.log("----- standard request -----");
    const completion = await openai.chat.completions.create({
      messages: [
        {
          role: "system",
          content: "你是豆包，是由字节跳动开发的 AI 人工智能助手",
        },
        { role: "user", content: content },
      ],
      model: "ep-20250210144414-rf9qw",
    });
   return (completion.choices[0]?.message?.content);
  }

  const result = await main();
  ctx.body = {
    code: 200,
    data: result,
  }
});

router.get("/string", async (ctx, next) => {
  ctx.body = "koa2 string";
});

router.get("/json", async (ctx, next) => {
  ctx.body = {
    title: "koa2 json",
  };
});

module.exports = router;
